
"""
世界观分析
"""
from models import Chapter
from pydantic import Field
from core.utils import BaseAnalyzer, prompt_dir


class WorldviewAnalyzer(BaseAnalyzer):

    def __init__(self):
        super().__init__(prompt_file=prompt_dir / 'context/c4. 世界观构建分析.md')

    def invoke(
            self,
            last_chapter: Chapter = Field(description='上一章节的世界观'),
            current_chapter: Chapter = Field(description="当前章节内容")
    ):

        if isinstance(last_chapter.worldview, dict) and 'worldview' in last_chapter.worldview:
            established_worldview = last_chapter.worldview['worldview']
        else:
            established_worldview = last_chapter.worldview

        prompt = self.template_prompt.format(
            inputs={
                "established_worldview": established_worldview,
                "chapter_content": current_chapter.chapter_content
            },
            remove_template_variables=True
        )
        current_chapter.worldview = self.analyze(prompt)

        return current_chapter.worldview
